package com.totoro.transfer;

import com.totoro.algorithm.PerceptualHashAlgorithm;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class PhashDirTest {

    private static String dir = "/Volumes/test/fileDown/images/";
    private static PerceptualHashAlgorithm p = new PerceptualHashAlgorithm();
    static List<File> fileAll = new ArrayList<>();
    File or;
    File[] files;

    // 用于遍历文件价
    public void iteratorPath(String dir) {
        or = new File(dir);
        files = or.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    if (!file.getName().contains(".DS_Store")) {
                        try {
                            fileAll.add(file);
                        } catch (Exception e) {
                            System.out.println(e);
                        }
                    }
                } else if (file.isDirectory()) {
                    iteratorPath(file.getAbsolutePath());
                }
            }
        }
    }

    public static void main(String[] args) {
        PhashDirTest t = new PhashDirTest();
        t.iteratorPath(dir);
        for (int k = 0; k < fileAll.size(); k = k + 300) {
            Handler h = new Handler();
            for (int m = 0; m < 300; m++) {
                h.getFileList().add(fileAll.get(k + m));
            }
            h.start();
        }

    }

}


class Handler extends Thread {

    private List<File> fileList = new ArrayList();

    public List<File> getFileList() {
        return fileList;
    }

    public void setFileList(List<File> fileList) {
        this.fileList = fileList;
    }

    private PerceptualHashAlgorithm p = new PerceptualHashAlgorithm();

    public void run() {
        for (int i = 0; i < fileList.size(); i++) {
            try {
                System.out.println(Thread.currentThread().getName()+ "//"+p.getHash(new FileInputStream(fileList.get(i))));
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

}